package com.ydd.controller.user;

import com.baomidou.mybatisplus.extension.api.R;
import com.ydd.YddApplication;
import com.ydd.dto.*;
import com.ydd.entity.YddArticle;
import com.ydd.entity.YddUser;
import com.ydd.result.PageResult;
import com.ydd.result.Result;
import com.ydd.service.UserArticleService;
import com.ydd.vo.DataListVO;
import com.ydd.vo.DataListsVO;
import com.ydd.vo.ReturnVO;
import com.ydd.vo.UserArticleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/user")
@Api(tags = "用户文章管理接口")
@Slf4j //日志记录
@CrossOrigin
public class UserArticleController {

    @Autowired
    private UserArticleService userArticleService;


    /**
     * 用户中心
     */
    @GetMapping("/getUserInfo")
    @ApiOperation("用户中心")
    public Result<UserInfoDTO> info() {
        UserInfoDTO userInfoDTOs = userArticleService.info();
        return Result.success(userInfoDTOs);
    }

    /**
     * 我的文章
     *
     * @param userArticleRequestDTO
     * @return
     */

    @GetMapping("/myArticle")
    @ApiOperation("我的文章")
    public Result<UserArticleDTO> myArticle(UserArticleRequestDTO userArticleRequestDTO) {
        log.info("我的文章+{}" + userArticleRequestDTO);
        UserArticleDTO userArticleDTO = userArticleService.MyArticle(userArticleRequestDTO);
        return Result.success(userArticleDTO);
    }

    /**
     * 我的收藏
     *
     * @ param
     */

    @GetMapping("/userDataList")
    @ApiOperation("我的收藏")
    public Result<DataListsVO> userDataList(DataListDTO dataListDTO) {
        log.info("我的收藏+{}" + dataListDTO);
        DataListVO dataListVO = userArticleService.userDataList(dataListDTO);
        DataListsVO list = new DataListsVO();
        list.setList(dataListVO);
        return Result.success(list);
    }
    /**
     * 取消收藏
     */
    @GetMapping("/userDataHandle")
    @ApiOperation("取消收藏")
    public Result<String> update(CancelListDTO cancelListDTO) {
        log.info("取消收藏+{}" + cancelListDTO);
        userArticleService.cancel(cancelListDTO);
        ReturnVO returnVO = new ReturnVO();
        return Result.success("操作成功");
    }

    /**
     * 修改用户资料
     */
    @PostMapping("/update")
    @ApiOperation("修改用户资料")
    public Result update(@RequestBody YddUser yddUser) {
        log.info("修改用户资料+{}" + yddUser);
        userArticleService.update(yddUser);
        return Result.success();
    }

    /**
     * 发布文章
     */
    @CacheEvict(value = "newCache",allEntries = true )// 发布文章 删除最新文章缓存
    @PostMapping("/addArticle")
    @ApiOperation("发布文章")
    public Result add(@RequestBody NewArticleDTO newArticleDTO) {
        log.info("上传文章+{}" + newArticleDTO);
        userArticleService.insert(newArticleDTO);
        return Result.success();
    }

    /**
     * 删除文章
     */
    @GetMapping("delArticle")
    @ApiOperation("删除文章")
    public Result del(Long id) {
        log.info("文章删除+{}" + id);
        userArticleService.del(id);
        return Result.success();

    }

    /**
     * 修改文章
     *
     */
    @PostMapping("/editArticle")
    @ApiOperation("修改文章")
    public Result edit(@RequestBody DraftDTO draftDTO){
        log.info("修改文章+{}"+draftDTO);
        userArticleService.edit(draftDTO);
        return Result.success();
    }

}
